package com.example.onlinetaxi.ui.activity.Driver.MainPage.MineActivities;

import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class DriverMineCarActivity extends BaseActivity {
    // 下拉框组件
    Spinner brandSpinner;
    Spinner colorSpinner;
    private EditText carNumberET;
    // 按钮
    private TextView cancel;
    private TextView modify;

    // 汽车品牌
    private String carBrand;
    // 汽车颜色
    private String carColor;
    // 汽车牌号
    private String carNumber;
    // 请求参数
    private Map<String, String> map = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_mine_car);

        // 获取车辆信息
        getCarInfo();

        initView();

        initEvent();
    }

    private void getCarInfo() {
        map.clear();
        map.put("phone", LocalStorage.phone);

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "driver", "getCarInfo");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            carNumber = dataResponse.getData().get("carNumber").toString();
            carBrand = dataResponse.getData().get("carBrand").toString();
            carColor = dataResponse.getData().get("carColor").toString();
        }
    }

    private void initView() {
        // 选择器
        brandSpinner = findViewById(R.id.mine_car_brand_spinner);
        colorSpinner = findViewById(R.id.mine_car_color_spinner);
        // 按钮
        cancel = findViewById(R.id.mine_car_cancel);
        modify = findViewById(R.id.mine_car_confirm);
        carNumberET = findViewById(R.id.mine_car_number);

        // 自动换行
        //设置EditText的显示方式为多行文本输入
        carNumberET.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        //文本显示的位置在EditText的最上方
        carNumberET.setGravity(Gravity.TOP);//改变默认的单行模式
        //改变默认的单行模式
        carNumberET.setSingleLine(false);
        //水平滚动设置为False
        carNumberET.setHorizontallyScrolling(false);

        // 初始化所有值
        setCarInfo();
    }

    private void setCarInfo() {
        if (carColor != null && !carColor.equals("")) {
            SpinnerAdapter apsAdapter = colorSpinner.getAdapter();
            int k = apsAdapter.getCount();
            for (int i = 0; i < k; i++) {
                if (carColor.equals(apsAdapter.getItem(i).toString())) {
                    colorSpinner.setSelection(i, true);
                    break;
                }
            }
        }
        if (carBrand != null && !carBrand.equals("")) {
            SpinnerAdapter apsAdapter = brandSpinner.getAdapter();
            int k = apsAdapter.getCount();
            for (int i = 0; i < k; i++) {
                if (carBrand.equals(apsAdapter.getItem(i).toString())) {
                    brandSpinner.setSelection(i, true);
                    break;
                }
            }
        }
        if (carNumber != null && !carNumber.equals("")) {
            carNumberET.setText(carNumber);
        }
    }

    private void initEvent() {
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 验证是否输入正确
                if (isCorrect()) {
                    // 发起车牌号保存请求
                    if (saveCarInfo()) {
                        Toast.makeText(DriverMineCarActivity.this, "车辆信息修改成功！", Toast.LENGTH_SHORT).show();

                        // 关闭当前页面
                        finish();
                    }
                }
            }
        });
        // 品牌选择器
        brandSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                carBrand = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getApplicationContext(), "onNothingSelected", Toast.LENGTH_LONG).show();
            }
        });
        // 颜色选择器
        colorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                carColor = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getApplicationContext(), "onNothingSelected", Toast.LENGTH_LONG).show();
            }
        });
    }

    private boolean saveCarInfo() {
        map.clear();
        map.put("phone", LocalStorage.phone);
        map.put("carNumber", carNumber);
        map.put("carColor", carColor);
        map.put("carBrand", carBrand);

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "driver", "saveCarInfo");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            return true;
        }
        return false;
    }

    private boolean isCorrect() {
        // 车牌号
        carNumber = carNumberET.getText().toString();

        if (carColor.equals("请选择汽车颜色")) {
            Toast.makeText(DriverMineCarActivity.this, "请选择汽车颜色！", Toast.LENGTH_SHORT).show();
            return false;
        } else if (carBrand.equals("请选择汽车品牌")) {
            Toast.makeText(DriverMineCarActivity.this, "请选择汽车品牌！", Toast.LENGTH_SHORT).show();
            return false;
        } else if (carNumber == null | checkPlateNumberFormat(carNumber)) {
            Toast.makeText(DriverMineCarActivity.this, "您输入的汽车牌号格式不正确！", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    public boolean checkPlateNumberFormat(String carNumber) {
        String pattern = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";
        return Pattern.matches(pattern, carNumber);
    }
}